<template>
  <div class="modle" v-show="$store.state.isOpenLogin">
    <div class="modle_bg" @click="gbLogin"></div>

    <div class="modal_content">
      <img
        class="close"
        @click="gbLogin"
        src="../assets/images/login/userArrow.png"
        alt=""
      />
      <section>
        <span
          :class="{ active: kzPhoneLoginAndVxLogin }"
          @click="kzPhoneLoginAndVxLogin = true"
          >手机号登录</span
        >
        <span :class="{ active: !kzPhoneLoginAndVxLogin }" @click="vxLong"
          >微信扫码登录</span
        >
      </section>
      <div class="phoneLogin" v-show="kzPhoneLoginAndVxLogin">
        <div class="tanxin line">
          <input
            type="text"
            name=""
            id=""
            placeholder="请输入手机号码"
            v-model="phoneText"
          />
        </div>
        <!-- 滑块验证码 -->
        <div class="verify_box">
          <slide-verify
            :l="42"
            :r="10"
            :w="360"
            :h="155"
            slider-text="向右滑动"
            @success="onSuccess"
            @fail="onFail"
            @refresh="onRefresh"
          ></slide-verify>
          <div>{{ msg }}</div>
        </div>
        <div class="tanxin line">
          <input type="text" v-model="code" />
          <div class="btn" @click="huoquyanzhengma1" v-show="huoquyanzhengmaIF">
            获取验证码
          </div>
          <div class="btn btn2" v-show="huoquyanzhengmaIF == false">
            {{ huoquyanzhengmadaojisi }} 秒
          </div>
        </div>
        <div class="btn_login" @click.stop="submitfn">登录</div>
      </div>
      <div class="vxLogin" v-show="!kzPhoneLoginAndVxLogin" id="weixin"></div>
    </div>
  </div>
</template>

<script>
import {
  getCodedataApi,
  LogindataApi,
  getUresInfoApi,
  bindWechatPhoneApi,
} from "@/request/api";
const vaildatephone = (phoneText) => {
  let reg = /^1(3\d|4[5-9]|5[0-3,5-9]|6[2,5,6,7]|7[0-8]|8\d|9[0-3,5-9])\d{8}$/;
  return reg.test(phoneText.trim());
};
export default {
  data() {
    return {
      msg: "",
      // 手机号
      phoneText: "16602812545",
      // 验证码
      code: "9142",
      // 控制获取验证码按钮
      huoquyanzhengmaIF: true,
      huoquyanzhengmadaojisi: 60,
      // 手机登录和微信登录切换
      kzPhoneLoginAndVxLogin: true,
      // 控制
    };
  },
  methods: {
    gbLogin() {
      this.$store.commit("changeIsOpenLogin", false);
    },
    onSuccess() {
      this.msg = "验证成功";
    },
    onFail() {
      this.msg = "验证失败";
    },
    onRefresh() {
      this.msg = "";
    },
    quchutk() {
      this.isPhoneTextshow = false;
    },
    // 点击获取验证码
    huoquyanzhengma1() {
      let toastObj = {
        content: "",
        icon: "",
      };
      if (this.phoneText == "") {
        toastObj.content = "请输入手机号";
        toastObj.icon = "danger";
        this.$store.dispatch("AsyncToast", toastObj);
        return;
      }
      let result = vaildatephone(this.phoneText);
      if (!result) {
        toastObj.content = "请输入正确的手机号";
        toastObj.icon = "danger";
        this.$store.dispatch("AsyncToast", toastObj);
        return;
      }
      if (this.msg != "验证成功") {
        toastObj.content = "请完成拼图验证";
        toastObj.icon = "danger";
        this.$store.dispatch("AsyncToast", toastObj);
        return;
      }
      getCodedataApi({
        phone: this.phoneText,
      }).then((res) => {
        if (res.data.code === 0) {
          toastObj.content = res.data.message;
          toastObj.icon = "success";
          this.$store.dispatch("AsyncToast", toastObj);

          this.huoquyanzhengmaIF = false;
          let kzm = setInterval(() => {
            this.huoquyanzhengmadaojisi--;
          }, 1000);

          setTimeout(() => {
            this.huoquyanzhengmaIF = true;
            clearInterval(kzm);
            this.huoquyanzhengmadaojisi = 60;
          }, 60000);
        } else {
          toastObj.content = res.data.message;
          toastObj.icon = "danger";
          this.$store.dispatch("AsyncToast", toastObj);
        }
      });
    },
    // 点击登录
    submitfn() {
      let toastObj = {
        content: "",
        icon: "",
      };
      if (this.phoneText == "") {
        toastObj.content = "请输入手机号";
        toastObj.icon = "danger";
        this.$store.dispatch("AsyncToast", toastObj);
        return;
      }
      let result = vaildatephone(this.phoneText);
      if (!result) {
        toastObj.content = "请输入正确的手机号";
        toastObj.icon = "danger";
        this.$store.dispatch("AsyncToast", toastObj);
        return;
      }
      if (this.msg != "验证成功") {
        toastObj.content = "请完成拼图验证";
        toastObj.icon = "danger";
        this.$store.dispatch("AsyncToast", toastObj);
        return;
      }
      if (this.code == "") {
        toastObj.content = "请输入验证码";
        toastObj.icon = "danger";
        this.$store.dispatch("AsyncToast", toastObj);
        return;
      }

      // 刚才我们知道  如果存在 uuid 的东西 ，就是微信扫码登录得到的
      // 如果不存在 uuid  就是 手机号登录

      // 存在uuid
      let uuid = sessionStorage.getItem("uuid");
      if (uuid) {
        // 如果存在 uuid 的东西 ，证明就是微信扫码登录得到的
        bindWechatPhoneApi({
          phone: this.phoneText,
          verifyCode: this.code,
          uuid: uuid,
        }).then((res) => {
          if (res.data.data === 0) {
            //已经登录成功
            // 存储token
            localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
            // 提示登录成功
            let toastObj = {
              content: res.data.message,
              icon: "success",
            };
            this.$store.dispatch("AsyncToast", toastObj);
            // 关闭提示窗
            this.$store.commit("changeIsLogin", false);
            // 更新header组件
            this.$emit("updatakey");
            // 删除地址栏的code
            this.$router.push(this.$route.path);
            // 删除uuid
            sessionStorage.removeItem("uuid");
          }
        });
      } else {
        LogindataApi({
          verifyCode: this.code,
          phone: this.phone,
        }).then((res) => {
          if (res.data.code === 0) {
            // 存储token
            localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
            // 提示登录成功
            let toastObj = {
              content: res.data.message,
              icon: "success",
            };
            this.$store.dispatch("AsyncToast", toastObj);
            // 关闭登录框
            this.$store.commit("changeIsLogin", false);
            // 更新header组件
            this.$emit("updateKeyFn");
          }
        });
      }

      LogindataApi({
        verifyCode: this.code,
        phone: this.phoneText,
      }).then((res) => {
        if (res.data.code === 0) {
          // 将toke放到本地存储上
          localStorage.setItem("x-auth-token", res.data["x-auth-token"]);

          toastObj.content = res.data.message;
          toastObj.icon = "success";
          this.$store.dispatch("AsyncToast", toastObj);

          this.$store.commit("changeIsOpenLogin", false);

          //修改header中的信息
          getUresInfoApi().then((res) => {
            if (res.data.code === 0) {
              this.$store.commit("chcartTotal", res.data.data.cartTotal);
              this.$store.commit("changeisgouwucheAndLogin", true);
              this.$store.commit("chUresInfo", res.data.data.userInfo);
            }
          });
        }
      });
    },
    vxLong() {
      this.kzPhoneLoginAndVxLogin = false;
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa(process.env.VUE_APP_STATE_URL + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,Lyogd3hsb2dpbi5jc3MgKi8NCi5pbXBvd2VyQm94IC50aXRsZSwNCi5pbXBvd2VyQm94IC5pbmZvIHsNCiAgICBkaXNwbGF5OiBub25lOw0KfQ0KDQouaW1wb3dlckJveCAucXJjb2RlIHsNCiAgICBtYXJnaW4tdG9wOiAyMHB4Ow0KfQ==",
      });
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "@/assets/base.less";
.modle {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  .modle_bg {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
  }
  .modal_content {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    width: 555px;
    height: 423px;
    background-image: url(../assets/images/login/loginBg.png);

    .close {
      position: absolute;
      right: 60px;
      top: 20px;
    }
    section {
      display: flex;
      margin-top: 50px;
      justify-content: center;
      margin-bottom: 30px;
      span {
        color: #ccc;
        cursor: pointer;
        &:nth-of-type(1) {
          border-radius: 1px solid #ccc;
          padding-right: 10px;
          margin-right: 10px;
        }
        &.active {
          color: #333;
        }
      }
    }
    .line {
      width: 360px;
      margin: 0 auto 20px;
      height: 50px;
      input {
        flex: 1;
        text-indent: 1em;
        outline: none;
      }
      .btn {
        width: 90px;
        height: 50px;
        text-align: center;
        background-color: @blue;
        color: #fff;
        cursor: pointer;
        line-height: 50px;
        margin-left: 10px;
      }
      .btn2 {
        background-color: #ccc;
        color: @blue;
      }
    }
    .btn_login {
      width: 360px;
      height: 50px;
      text-align: center;
      background-color: @blue;
      color: #fff;
      cursor: pointer;
      line-height: 50px;
      margin-left: 10px;
      margin: auto;
    }
    .verify_box {
      width: 360px;
      margin: 0 auto 20px;
      /deep/.slide-verify {
        width: 100% !important;
        canvas {
          position: absolute;
          top: -150px;
          left: 0;
          display: none;
          box-sizing: border-box;
        }
        .slide-verify-refresh-icon {
          position: absolute;
          top: -150px;
          display: none;
        }
        &:hover {
          canvas {
            display: block;
          }
          .slide-verify-refresh-icon {
            display: block;
          }
        }
      }
    }
    .vxLogin {
      width: 300px;
      margin: 0 auto;

      .impowerBox {
        width: 200px;
      }
    }
  }
}
</style>